var requestManager = require('../../../utils/requestManager.js')
var util = require('../../../utils/util.js')
var rcRecruitmentId = ''

Page({

  /**
   * 页面的初始数据
   */
  data: {
    contactList: [],
    hidden: true,
    modalInputValue: '',
    selectIndex: -1,
    selectId: '',
    nextUrl: ''
  },
  /////////////// 公有方法 ///////////////
  /** 
   * 将网络请求活动数据转化成UI需要的数据模型数组
   */
  resArrayConverToObjectList: function (resArray, dataArray) {
    for (var i = 0; i < resArray.length; i++) {
      let dic = resArray[i]

      let applyTime = util.formatUTCDateByFormatStr(dic["create_time"], 'YYYY-MM-DD')
      // 创建联系人信息
      let item = {
        'id': dic['id'],
        'name': dic['user_nick'],
        'ipone': dic['user_phone'],
        'time': applyTime,
        'contacted': dic['contact'],
        'mark': dic['remarks']
      }
      // 添加到联系人信息数组
      dataArray.push(item)
    }
  },
  /////////////// 公有方法 ///////////////

  /////////////// 网络请求 ///////////////
  /** 
   * 招聘信息列表 
   * recruitmentId 招聘id
   */
  requestContactList: function (recruitmentId) {
    var that = this
    requestManager.getRequest('recruitment/contact/info/list?id=' + recruitmentId, {}, function (res) {
      console.log(res)
      let status = res.status
      if (status == 0) {
        let array = res.results
        that.data.nextUrl = res.next
        var contactArray = []
        // 将网络请求活动数据转化成UI需要的数据模型数组
        that.resArrayConverToObjectList(array, contactArray)
        console.log(contactArray)
        that.setData({
          contactList: contactArray
        })
      }
    })
  },

  /** 
   * 联系或已联系
   * id 联系id
   */
  requestContactOrContacted: function (id, selectIndex) {
    var that = this
    let contacted = this.data.contactList[selectIndex].contacted
    if (!contacted) {
      requestManager.postRequest('recruitment/contact/info/contact?id=' + id, {}, function (res) {
        let status = res.status
        if (status == 0) {
          // 操作成功
          wx.showToast({
            title: '操作成功！',
            icon: 'none'
          })
          console.log('联系成功！')
          let key = 'contactList[' + selectIndex + '].contacted'
          that.setData({
            [key]: !contacted
          })
        }
      })
    }
  },

  /**
   * 添加备注
   * id 联系人id
   */
  requestMark: function (id, mark) {
    var that = this
    requestManager.postRequest('recruitment/contact/info/remarks?id=' + id, { 'remarks': mark}, function (res) {
      let status = res.status
      if (status == 0) {
        // 添加备注成功
        let mark = that.data.modalInputValue
        let key = 'contactList[' + that.data.selectIndex + '].mark'
        that.setData({
          [key]: mark,
          selectId: '',
          modalInputValue: '',
          selectIndex: -1
        })
      }
    })
  },

  /**
   * 上拉加载请求
   */
  requestNextContactList: function (nextUrl) {
    var that = this
    console.log('nextUrl' + nextUrl)
    let hasff = 'https://wx.extend.xuemei99.com/'
    let next = nextUrl.substring(hasff.length)
    
    requestManager.getRequest(next, {}, function (res) {
      let status = res.status
      if (status == 0) {
        var contactArray = that.data.contactList
        let array = res.results
        that.data.nextUrl = res.next
        // 将网络请求活动数据转化成UI需要的数据模型数组
        that.resArrayConverToObjectList(array, contactArray)
        console.log(contactArray)
        that.setData({
          contactList: contactArray
        })
      }
    })
  },

  /////////////// 网络请求 ///////////////


  /////////////// 事件 ///////////////
  /* 联系或已联系 */
  contactTap: function (event) {
    console.log(event)
    let that = this;
    let user_phone = event.currentTarget.dataset.phone
    console.log(user_phone)
    if (user_phone == "" || user_phone == undefined) {
      wx.showToast({
        title: '手机号码为空',
        icon: "none"
      })
    } else {
      wx.makePhoneCall({
        phoneNumber: user_phone,
      })

      let index = event.currentTarget.dataset.index;
      let id = event.currentTarget.dataset.id
      that.requestContactOrContacted(id, index)
    }
  },

  /* 备注 */
  markTap: function (event) {
    let mark = event.currentTarget.dataset.mark
    let index = event.currentTarget.dataset.index
    let id = event.currentTarget.dataset.id
    this.setData({
      selectId: id,
      modalInputValue: mark,
      selectIndex: index,
      hidden: false
    });
  },

  /* 弹框取消事件 */
  cancel: function () {
    this.setData({
      selectId: '',
      modalInputValue: '',
      selectIndex: -1,
      hidden: true
    });
  },
  /* 弹框确定事件 */
  confirm: function () {
    var that = this
    this.setData({
      hidden: true
    });
    let id = this.data.selectId
    let mark = this.data.modalInputValue
    this.requestMark(id, mark)
  },

  /* 输入框监控 */
  modalInput: function (event) {
    this.data.modalInputValue = event.detail.value
  },
  /////////////// 事件 ///////////////
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    rcRecruitmentId = options.recruitment_id
    console.log('rcRecruitmentId :' + rcRecruitmentId)
    this.requestContactList(rcRecruitmentId)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('下拉刷新')
    this.requestContactList(rcRecruitmentId)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('上拉加载')
    let tNextUrl = this.data.nextUrl
    if (tNextUrl && tNextUrl.length > 0) {
      this.requestNextContactList(tNextUrl)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})